package com.example.nio.utils;

import com.example.nio.commons.Agent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class AgentUserUtil {

	private static final Logger logger = LoggerFactory.getLogger(AgentUserUtil.class);
	// 绑定客服与用户 String：
	public static Map<String, Agent> agentUser = new Hashtable<>();

	public static Map<String,Boolean> agentUserMessage = new Hashtable<>();

	//满意度
	public static Map<String,String> satisfyList = new Hashtable<>();

	/**
	 * 检查用户是否已与客服建立联系
	 * @param toUserName
	 * @return 已建立返回true，否则false
	 */
	public static boolean checkUser(String toUserName) {
		return agentUser.containsKey(toUserName);
	}

	// 添加用户
	public static void addUser(String toUserName, Agent agent) {
		agentUser.put(toUserName, agent);
	}
	// 看看当前聊天窗口最后发送的消息是否是客服发的
	public static Boolean checkMessageLast(String toUserName){
		return agentUserMessage.get(toUserName);
	}

	// 看看当前聊天窗口最后发送的消息是否是客服发的
	public static void setLastMessage(String toUserName,Boolean isLast){
		agentUserMessage.put(toUserName,isLast);
	}


	// 删除客服
	public static List<String> deleteAgent(Agent agent) {
		for (Iterator<Map.Entry<String, Agent>> it = agentUser.entrySet().iterator(); it.hasNext();){
			Map.Entry<String, Agent> item = it.next();
			String key = item.getKey();
			Agent val = item.getValue();
			if (val.equals(agent)) {
//                users.add(key);
				it.remove();
			}
		}
		return null;
	}


	// 删除用户
	public static String deleteUser(String toUserName) {
		for (Iterator<Map.Entry<String, Agent>> it = agentUser.entrySet().iterator(); it.hasNext();){
			Map.Entry<String, Agent> item = it.next();
			if (toUserName.equals(item.getKey())) {
//                users.add(key);
				it.remove();
				logger.info("删除用户:"+toUserName);
			}
		}
		return toUserName;
	}


	// 根据用户查找客服
	public static Agent getAgentByUser(String toUserName) {
		return agentUser.get(toUserName);
	}

	//根据客服id查询客服
	public static Agent getAgentById(String agentId){
		for(String agents:agentUser.keySet()){
			if(agentUser.get(agents).getAgent().equals(agentId)){
				return agentUser.get(agents);
			}
		}
		return null;
	}

	//根据客服查找用户
	public static List<String> getUserByAgent(Agent agent){
		List<String> userList=new ArrayList<String>();
		for (String key : agentUser.keySet()) {
			if(agentUser.get(key).equals(agent)){
				userList.add(key);
			}
		}
		return userList;
	}

	// 根据客服修改channel
	public static void updateAgentChannel(Agent agent){
		for (String key : agentUser.keySet()) {
			if(agentUser.get(key).getAgent().equals(agent.getAgent())){
				agentUser.put(key,agentUser.get(key).setChannel(agent.getChannel()));
			}
		}
	}

	//判断满意度用户是否存在
	public static boolean checkSatisfy(String openId){
		return satisfyList.get(openId) != null;
	}
	//判断满意度用户是否对应的接待id
	public static String getStatisticsId(String openId){
		return satisfyList.get(openId);
	}

	//添加满意度用户
	public static void addSatisfy(String openId,String statisticsId){
		satisfyList.put(openId,statisticsId);
	}

	//删除满意度用户
	public static void deletetSatisfy(String openId){
		satisfyList.remove(openId);
	}
}
